<template>
  <div class="main">
    <div class="left">
      <studentMainleft></studentMainleft>
    </div>
    <div class="rmain">
      <div class="right" v-if="user.PositionId!=6">
        <div class="downloadexcel">
          <!--导出用户信息excel表格-->
          <download-excel
            class="export-excel-wrapper"
            :data="json_data"
            :fields="json_fields"
            name="用户信息列表.xls"
          >
            没有模板？点击此下载
            <el-link type="primary" style="font-size: 18px">学生信息模板</el-link>
          </download-excel>
        </div>
        <div class="uploadexcel">
          <vue-xlsx-table @on-select-file="handleSelectedFile">上传学生信息</vue-xlsx-table>
        </div>
      </div>
      <div class="right1">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane label="我的学生" name="first">
            <template>
              <el-table
                :data="stuList"
                style="width: 100%"
                max-height="550"
                @cell-dblclick="cellClick"
              >
                <el-table-column prop="OpeningDate" label="开课日期" width="180"></el-table-column>
                <el-table-column label="授课老师" width="300" align="center">
                  <template slot-scope="scope">
                    <div v-for="item in Workers" :key="item.WorkerId">
                      <span v-if="item.WorkerId==scope.row.WorkerId">{{item.WorkerName}}</span>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="授课班级" width="300" align="center" prop="ClassId"></el-table-column>
                <el-table-column label="学生名称" width="300" align="center">
                  <template slot-scope="scope">
                    <div v-for="item in Student" :key="item.Id">
                      <span v-if="item.StuId==scope.row.StuId">{{item.StuName}}</span>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="课程" width="300" align="center">
                  <template slot-scope="scope">
                    <div v-for="item in Course" :key="item.CourseId">
                      <span v-if="item.CourseId==scope.row.CourseId">{{item.CourseName}}</span>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="成绩" prop="Score"></el-table-column>
                <el-table-column
                  width="156"
                  label="操作"
                  align="center"
                  prop="ClassId"
                  header-align="center"
                >
                  <template slot-scope="scope">
                    <span style="margin-left: 10px">
                      <el-link type="primary" @click="editruleform(scope.row)">修改</el-link>
                    </span>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="添加学生" name="second" v-if="stuInfo != ''">
            <template>
              <el-table
                :data="stuInfo"
                style="width: 100%"
                max-height="550"
                @cell-dblclick="cellClick"
              >
                <el-table-column width="120" label="学生姓名" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.StuName"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuName
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="100" label="学生年龄" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.StuAge"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuAge
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="100" label="学生性别" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-select
                      @change="change(scope.row)"
                      v-model="scope.row.StuSex"
                      v-if="scope.row.seen == true"
                    >
                      <el-option
                        v-for="item in sex"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      ></el-option>
                    </el-select>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuSex
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="140" label="学生电话" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.StuPhone"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuPhone
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="学生电子邮件" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.StuEmail"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuEmail
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="180" label="学生身份证" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.StuCarid"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StuCarid
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="班级" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      disabled
                      v-model="scope.row.ClassId"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span
                      style="margin-left: 10px;color:red"
                      v-else-if="scope.row.ClassId!=user.ClassId"
                      flag="true"
                    >
                      {{
                      scope.row.ClassId
                      }}
                    </span>
                    <span style="margin-left: 10px;" v-else>
                      {{
                      scope.row.ClassId
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="入学时间" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-date-picker
                      type="date"
                      v-model="scope.row.StubackTime"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-date-picker>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.StubackTime
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="政治面貌" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-select
                      @change="change(scope.row)"
                      v-model="scope.row.PoliticalLandscape"
                      v-if="scope.row.seen == true"
                    >
                      <el-option
                        v-for="item in PoliticalLandscape"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      ></el-option>
                    </el-select>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.PoliticalLandscape
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="常住地" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.NativePlace"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.NativePlace
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="130" label="民族" align="center" header-align="center">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.National"
                      v-if="scope.row.seen == true"
                      @blur="loseFcous(scope.$index, scope.row)"
                    ></el-input>

                    <span style="margin-left: 10px" v-else>
                      {{
                      scope.row.National
                      }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column width="156" label="操作" align="center" header-align="center">
                  <template slot-scope="scope">
                    <span style="margin-left: 10px">
                      <el-link type="primary" @click="cellClick(scope.row)">修改</el-link>
                      <el-link type="primary" @click="deletestu(scope.$index, scope.row)">删除</el-link>
                    </span>
                  </template>
                </el-table-column>
              </el-table>
            </template>
            <div class="btm">
              <div class="bl">
                <!-- <span>*注意：双击学生信息可进行修改</span> -->
              </div>
              <div class="bl">
                <el-button type="primary" @click="subsutdent()">确认提交</el-button>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <el-dialog title="学生信息" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓名" prop="StuName">
          <el-input v-model="ruleForm.StuName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="StuPhone">
          <el-input v-model="ruleForm.StuPhone"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="StuSex">
          <el-select v-model="ruleForm.StuSex" placeholder="请选择性别" style="width: 100%;">
            <el-option label="男" value="male"></el-option>
            <el-option label="女" value="female"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="StuAge">
          <el-input v-model.number="ruleForm.StuAge"></el-input>
        </el-form-item>
        <el-form-item label="入学时间" prop="StubackTime">
          <el-date-picker
            type="date"
            v-model="ruleForm.StubackTime"
            style="width: 100% !important;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="邮箱" prop="StuEmail">
          <el-input v-model="ruleForm.StuEmail"></el-input>
        </el-form-item>
        <el-form-item label="班级" prop="ClassId">
          <el-input v-model="ruleForm.ClassId"></el-input>
        </el-form-item>
        <el-form-item label="政治面貌" prop="ProfessionalName ">
          <el-select v-model="ruleForm.PoliticalLandscape" style="width: 100% !important;">
            <el-option
              v-for="item in PoliticalLandscape"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="籍贯" prop="NativePlace ">
          <el-input v-model="ruleForm.NativePlace"></el-input>
        </el-form-item>
        <el-form-item label="民族" prop="National ">
          <el-input v-model="ruleForm.National"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交申请</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import studentMainleft from "@/components/studentMainleft";
export default {
  components: {
    studentMainleft
  },
  data() {
    return {
      ruleForm: {
        StuName: "",
        StuId: "",
        StuPhone: "",
        StuSex: "",
        StuAge: "",
        StubackTime: "",
        StuEmail: "",
        ClassId: "",
        StuCarid: "",
        ProfessionalName: "",
        NativePlace: "",
        National: ""
      },
      rules: {
        StuName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        StuPhone: [
          { required: true, message: "请输入手机号", trigger: "blur" }
        ],
        StuSex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        StuAge: [
          { required: true, message: "请输入年龄" },
          { type: "number", message: "年龄必须为数字值" }
        ],
        StubackTime: [
          { required: true, message: "请输入入学时间", trigger: "blur" }
        ],
        StuEmail: [
          { required: true, message: "请输入电子邮件", trigger: "blur" }
        ],
        StuClassId: [
          { required: true, message: "请输入班级", trigger: "blur" }
        ],
        Grade: [{ required: true, message: "请输入年纪", trigger: "blur" }]
        //college: [{ required: true, message: "请输入学院", trigger: "blur" }]
      },
      dialogFormVisible: false,
      sex: [{ value: "男", label: "男" }, { value: "女", label: "女" }],
      PoliticalLandscape: [
        { value: "群众", label: "群众" },
        { value: "团员", label: "团员" },
        { value: "党员", label: "党员" }
      ],
      activeName: "first",
      //导出excel
      json_fields: {
        StuName: "StuName",
        StuAge: "StuAge",
        StuSex: "StuSex",
        StuPhone: "StuPhone",
        StuEmail: "StuEmail",
        StuCarid: "StuCarid",
        ClassId: "ClassId",
        StubackTime: "StubackTime",
        PoliticalLandscape: "PoliticalLandscape",
        NativePlace: "NativePlace",
        National: "National",
        seen: "seen"
      },
      json_data: [
        {
          seen: false
        }
      ],
      json_meta: [
        [
          {
            " key ": " charset ",
            " value ": " utf- 8 "
          }
        ]
      ],
      stuInfo: [],
      header: [],
      user: "",
      stuList: []
    };
  },
  created() {
    this.getStudent();
  },
  mounted() {},
  methods: {
    submitForm(from) {
      this.$axios({
        method: "put",
        url: this.$api.UpdateStudent,
        headers: {
          "Content-Type": "application/json"
        },
        data: JSON.stringify(this.ruleForm)
      }).then(res => {
        if (res.data.message > 0 && res.data.code == 200) {
          this.$message({
            type: "success",
            message: "修改成功"
          });
        } else {
          this.$message({
            type: "error",
            message: "修改失败"
          });
        }
        this.dialogFormVisible = false;
      });
    },
    editruleform(row) {
      this.dialogFormVisible = true;
      this.ruleForm = row;
    },
    getStudent() {
      this.user = JSON.parse(sessionStorage.getItem("user"));
      if (this.user.PositionId == 6) {
        this.$axios({
          method: "get",
          url: this.$api.GetAllStudent
        }).then(res => {
          console.log(res);
          this.stuList = res.data.message;
        });
      } else {
        this.$axios({
          method: "get",
          url: this.$api.GetStudentByClassId,
          params: { token: this.user.Token }
        }).then(res => {
          console.log(res);
          this.stuList = res.data.message;
        });
      }
    },
    change(row) {
      row.seen = false;
      this.$forceUpdate();
    },
    deletestu(index, row) {
      this.stuInfo.splice(index, 1);
    },
    subsutdent() {
      this.stuInfo.forEach(element => {
        delete element["seen"];
      });
      //console.log(JSON.stringify(this.stuInfo));
      this.axios({
        method: "post",
        url: this.$api.InsertApplicationForm,
        params: {
          token: this.user.Token,
          InfoJSON: JSON.stringify(this.stuInfo),
          ApplicationType: 1,
          status: 2
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.$route.go();
        }
      });
    },
    handleSelectedFile(convertedData) {
      // convertedData.body.forEach(element => {
      //   element.seen = false;
      // });
      this.stuInfo = convertedData.body;
      this.header = convertedData.header;
    },
    cellClick(row, column) {
      row.seen = !row.seen;
    },
    loseFcous(index, row) {
      row.seen = false;
    }
  }
};
</script>

<style scoped>
.main {
  display: flex;
  height: 100%;
}
.rmain {
  width: 100%;
  height: 100%;
}
.right {
  width: 100%;
  background-color: white;
  height: 7%;
  margin-left: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 5px;
}
.right1 {
  width: 100%;
  background-color: white;
  height: 75%;
  margin-left: 10px;
}
.right1 .btm {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 25px 25px 0px 25px;
}

.right1 .btm .bl {
  color: red;
}
.right .downloadexcel {
  padding: 20px;
  text-align: left;
  display: flex;
}
.right .uploadexcel {
  padding: 20px;
}
.right .uploadexcel .xlsx-button {
  font-size: 15px !important;
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 136px !important;
}
</style>
